package com.schoolegard.services.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.schoolegard.services.MailConfigService;
import com.schoolgard.common.MailConstants;
import com.schoolgard.dao.MailConfigDao;
import com.schoolgard.entitybean.MailConfig;

public class MailConfigServiceImpl implements MailConfigService{
	private static Logger logger = Logger.getLogger(MailConfigServiceImpl.class);
	
	private MailConfigDao mailConfigDao;

	public void saveOrUpdate(MailConfig mailConfig) {
		mailConfigDao.createOrModify(mailConfig);
	}

	public MailConfig findConfig() {
		List<MailConfig> list = mailConfigDao.findAll();
		if(list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	public void setMailConfigDao(MailConfigDao mailConfigDao) {
		this.mailConfigDao = mailConfigDao;
	}
	
	public void initConfig(){
		MailConfig mailConfig = this.findConfig();
		if(mailConfig == null ) {
			return;
		}
		MailConstants.setServiceHost(mailConfig.getServiceHost());
		MailConstants.setAddress(mailConfig.getServiceAddress());
		MailConstants.setUserName(mailConfig.getUserName());
		MailConstants.setPassword(mailConfig.getPassword());
	}

}
